﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using Dao;

namespace Web.Page
{
    public partial class KC_WLPD_Edit : System.Web.UI.Page
    {
        public static readonly string SELECTED = "to_revise";

        protected KC_WLPD_Dao dao = new KC_WLPD_Dao();
        private List<vWLKC> list;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                list = (List<vWLKC>)Cache.Get(SELECTED);
                if (list != null)
                {
                    this.GridView1.DataSource = list;
                    this.GridView1.DataBind();
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            User user = Session["user"] as User;
            if (user == null)
            {
                string script = "<script type=\"text/javascript\">if (window.opener != null) { window.opener.location = \"Login.aspx\"; window.close(); } else if (window.top != window) { window.top.location = \"Login.aspx\"; } </script>";
                ClientScript.RegisterClientScriptBlock(GetType(), "redirect", script, false);
                //Response.Redirect(FormsAuthentication.LoginUrl);
            }

            Guid uid = user.Id;
            Guid did = user.UserInDepartments.FirstOrDefault().DepartmentId;

            List<vWLKC> list = (List<vWLKC>)Cache.Get(SELECTED);
            List<vWLKCEx> list2 = new List<vWLKCEx>();
            bool valid = true;
            string msg = "";

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                vWLKC v = list[i];
                vWLKCEx item = new vWLKCEx(v);
                TextBox tbNewSL = (TextBox)(row.Cells[0].FindControl("tbNewSL"));
                string newSLStr = tbNewSL.Text;

                if (!String.IsNullOrWhiteSpace(newSLStr))
                {
                    try
                    {
                        int newSL = Int32.Parse(newSLStr);
                        item.SL = newSL;
                        tbNewSL.ForeColor = Color.Black;
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        tbNewSL.ForeColor = Color.Red;
                        valid = false;
                        break;
                    }
                }

                TextBox tbNewAQKCSL = (TextBox)(row.Cells[0].FindControl("tbNewAQKCSL"));
                string newAQKCSLStr = tbNewAQKCSL.Text;
                if (!String.IsNullOrWhiteSpace(newAQKCSLStr))
                {
                    try
                    {
                        int newAQKCSL = Int32.Parse(newAQKCSLStr);
                        item.AQKCSL = newAQKCSL;
                        tbNewAQKCSL.ForeColor = Color.Black;
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        tbNewAQKCSL.ForeColor = Color.Red;
                        valid = false;
                        break;
                    }
                }

                list2.Add(item);
            }

            if (!valid)
            {
                this.lbResult.Text = "输入有问题，请先纠正。详细：" + msg;
            }
            else
            {
                bool res = dao.Revise(uid, did, list2);
                if (res)
                {
                    this.lbResult.Text = "修订成功，已更新库存。";
                }
                else
                {
                    this.lbResult.Text = "修订失败，未更新库存。";
                }
            }
        }
    }
}